<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 9:52
 */
namespace app\index\controller;

use think\Db;
use think\View;
use think\Controller;
use think\session;

class Goods  extends Controller
{
    //全部服务列表
    public function all()
    {
        return view('all');
    }
    public function category()
    {
        //服务分类
        $w = array(
            'parentId'=> 0,
            'isShow'  => 1,
            'isEnable' => 1,
        );
        $list=Db::name('service_category')->where($w)->order('addDateTime','asc')->select();
        foreach ($list as $k1=>$v1){
            $sn=Db::name('service_category')->where('parentId',$v1['serviceCategoryId'])->order('serialNo','asc')->select();

            foreach ($sn as $k2=>$v2){
                $data=Db::name('service')->where('serviceCategoryId',$v2['serviceCategoryId'])->select();
                $sn[$k2]['san'] = $data;
            }
            $list[$k1]['er'] = $sn;

        }
        $this->view->assign('sn', $list);
        return view('goodcategory');
    }
    public  function  findlowyer($providerId,$areaId){
        //查找律师头像
        $lvShiId = DB::name('service_category')->where(array('categoryName'=>'法律服务'))->value('serviceCategoryId');
        //服务商服务分类
        $provideGroupId = Db::name('provider_group')->where(array('providerId'=>$providerId))->column('provideGroupId');
        $isMe = in_array($lvShiId,$provideGroupId);
        //服务商类型
        $providerType = Db::name('provider')->where(array('providerId'=>$providerId))->value('providerType');
        //该服务商是否为律师
        if($isMe &&  $providerType==0){
            //是律师，查找服务商头像
            $industryExperience =  Db::name('provider')->where(array('providerId'=>$providerId))->value('industryExperience');
            $headImgFileId   = DB::name('member')->where('memberId',$providerId)->value('headImgFileId');
            $realName = DB::name('provider_data')->where('providerId',$providerId)->value('realName');
            $data['detailImgFile'] = filepath($headImgFileId);
            $data['detailImgType'] =  '律师';
            $data['detailImgMemberId']['industryExperience'] = $industryExperience;
            $data['detailImgMemberId']['memberId'] = $providerId;
            $data['detailImgMemberId']['realName'] = $realName;
            return $data;
        }else{
            //不是律师，查找区域服务商
            $providerList = Db::name('member')
                ->join('provider','member.memberId=provider.providerId')
                ->where(array('areaId'=>$areaId,'memberType'=>1,'providerType'=>0))->column('memberId');
            $lvShiList = array();
            foreach($providerList  as $k=>$v){
                //查询包含法律服务的服务商
                $provider_group =  Db::name('provider_group')
                    ->where(array('providerId'=>$v))
                    ->column('provideGroupId');
                if(in_array($lvShiId,$provider_group)){
                    array_push($lvShiList,$v);
                }
            }
            if(count($lvShiList) !=1 ){
                $data['detailImgFile'] ='';
                $data['detailImgType'] =  '产品';
                $data['detailImgMemberId'] = array();
                return $data;
            }else{
                $industryExperience =  Db::name('provider')->where(array('providerId'=>$lvShiList['0']))->value('industryExperience');
                $headImgFileId   = DB::name('member')->where('memberId',$lvShiList['0'])->value('headImgFileId');
                $realName = DB::name('provider_data')->where('providerId',$lvShiList['0'])->value('realName');
                $data['detailImgFile'] = filepath($headImgFileId);
                $data['detailImgType'] =  '律师';
                $data['detailImgMemberId']['industryExperience'] = $industryExperience;
                $data['detailImgMemberId']['memberId'] = $lvShiList['0'];
                $data['detailImgMemberId']['realName'] = $realName;
                return $data;
            }

        }
    }
    public function detail()
    {
        $serviceId = $_GET['serviceId'];
        $providerId = getProviderId();
        $areaId = DB::name('member')->where('memberId',$providerId)->value('areaId');
        $province=Db::name('area')->where('level',0)->select();
        $this->view->assign('province',$province);
        $city=Db::name('area')->where('level',1)->select();
        $this->view->assign('cit',$city);
        $area=Db::name('area')->where('level',2)->select();
        $this->view->assign('area',$area);
        if (!empty($serviceId)) {
            //获取产品内容
            $service = Db::name('service')
                ->join('files','service.iconFileId=files.filesId')
                ->where('serviceId',$serviceId)
                ->find();
            //服务上一级
            $cate = Db::name('service_category')->where('serviceCategoryId',$service['serviceCategoryId'])->find();
            //服务上上一级
            $classify = Db::name('service_category')->where('serviceCategoryId',$cate['parentId'])->find();
            if($classify["categoryName"] =='法律服务'){
                $service['classType'] = "法律服务";
                $data = $this-> findlowyer($providerId,$areaId);
                if($data['detailImgType'] =='律师'){
                    $service['detailImgFile'] =  $data['detailImgFile'];
                }
                $service['detailImgType'] =  $data['detailImgType'];
                $service['detailImgMember'] =  $data['detailImgMemberId'];
            }else{
                $service['classType'] = "其他";
                $service['detailImgType'] =  '产品';
                $service['detailImgMember'] =  '';
            }
            //初始服务商地区售价
            $where['serviceId'] = $serviceId;
            $where['providerId'] = $providerId;
            //获取霍尔果斯区域id
            $hareaId = Db::name('area')->where('areaName',"霍尔果斯")->where('level',2)->find();
            $hdId = Db::name('area')->where('areaId',$hareaId['parentId'])->find();
            $hpId =  Db::name('area')->where('areaId',$hdId['parentId'])->find();
            $search = Db::name('service')->where('serviceId',$serviceId)->whereLike('serviceName','%霍尔果斯%')->find();
            if (!empty($search)){
                if ($hareaId == $areaId){
                    $price = Db::name('servicer_provider')->where('providerId',$providerId)->where('serviceId',$serviceId)->value('sellPriceVariation');
                }else{
                    $price = Db::name('service_area')->where('serviceId',$serviceId)->where('areaId',$hareaId['areaId'])->value('sellPriceBase');
                }
                //sheng
                $dId = Db::name('area')->where('areaId',$hareaId['areaId'])->value('parentId');
                $pId = Db::name('area')->where('areaId',$dId)->value('parentId');
                $p = Db::name('area')->where('parentId',0)->select();
                $d = Db::name('area')->where('parentId',$pId)->select();
                $c = Db::name('area')->where('parentId',$dId)->select();
                $v = array(
                    'areaId'=>$hareaId['areaId'],
                    'dId'   =>$hdId['areaId'],
                    'pId'   =>$hpId['areaId'],
                );
            }else{
                $dId = Db::name('area')->where('areaId',$areaId)->value('parentId');
                $pId = Db::name('area')->where('areaId',$dId)->value('parentId');
                $p = Db::name('area')->where('parentId',0)->select();
                $d = Db::name('area')->where('parentId',$pId)->select();
                $c = Db::name('area')->where('parentId',$dId)->select();
                $v = array(
                    'areaId'=>$areaId,
                    'dId'   =>$dId,
                    'pId'   =>$pId
                );
                $price = Db::name('service_provider')->where($where)->value('sellPriceVariation');
                if (empty($price))
                {
                    $where1['serviceId'] = $serviceId;
                    $where1['areaId'] = $areaId;
                    $price = Db::name('service_area')->where($where1)->value('sellPriceBase');
                    if (empty($price)){
                        $price = "面议";
                    }
                }
            }
            //获取评价条数
            $reviewNum=Db::name('provider_review')->where('serviceId',$serviceId)->where('providerId',getProviderId())->count();
            $this->view->assign('reviewNum', $reviewNum);
            //获取服务的评价信息
            $data =Db::name('provider_review pr')
                ->join('member','member.memberId=pr.memberId')
                ->where('pr.providerId',$providerId)->where('pr.serviceId',$serviceId)->limit(10)->select();
            //获取会员所在地区
            foreach ($data as $key => $value) {
                $area = Db::name('area')->where('areaId', $value['areaId'])->find();
                $parent = Db::name('area')->where('areaId', $area['parentId'])->find();
                $data[$key]['area'] = $area['areaName'];
                $data[$key]['parea'] = $parent['areaName'];
            }
        }
        $this->view->assign('search',$search);
        $this->view->assign('p',$p);
        $this->view->assign('c',$c);
        $this->view->assign('d',$d);
        $this->view->assign('city',$v);
        $this->view->assign('evaluate',$data);
        $this->view->assign('price',$price);
        $this->view->assign('service',$service);
        $this->view->assign('cate',$cate);
        $this->view->assign('classify',$classify);
        return view('goodsdetail');

    }
    public function getPrice()
    {
        $diId = input('di');
        $serviceId = input('serviceId');
        $where['serviceId'] = $serviceId;
        $where['areaId'] = $diId;
        $price = Db::name('service_area')->where($where)->cache(false)->value('sellPriceBase');
        //如果地区没有价格 按照当前服务商的价格
        $data = array();
        if (empty($price))
        {
            $price = Db::name('service_provider')->where('providerId',getProviderId())->where('serviceId',$serviceId)->cache(false)->value('sellPriceVariation');
            if (empty($price)){
                $data['price'] = "该地区平台未设置基本价";
            }else{
                $data['price'] = $price;
            }
        }else{
            $data['price'] = $price;
        }
        //是法律咨询找律师
        $serviceCategoryId = Db::name('service')->where(array('serviceId'=>$serviceId))->value('serviceCategoryId');
        //查找父分类
        $serviceparentId =   Db::name('service_category')->where(array('serviceCategoryId'=>$serviceCategoryId))->value('parentId');
        $categoryName =  Db::name('service_category')->where(array('serviceCategoryId'=>$serviceparentId))->value('categoryName');
        $data['isForSale']=Db::name('service')->where('serviceId',$serviceId)->value('isForSale');
        if($categoryName != '法律服务' ){
            $data['type'] = '其他';
        }else{
            //查找图标
            $providerId = getProviderId();
            $areaId = Db::name('member')->where(array('memberId'=>$providerId))->value('areaId');
            $data['type'] = '法律服务';
            if($areaId == $diId){
                //选择区域为服务商区域
                $findlowyer= $this->findlowyer($providerId,$diId);
                $data['ImgFile'] =  $findlowyer['detailImgFile'];
                $data['detailImgType'] =  $findlowyer['detailImgType'];
                $data['detailImgMember'] =  $findlowyer['detailImgMemberId'];
            }else{
                //其他区域
                $lvShiId = DB::name('service_category')->where(array('categoryName'=>'法律服务'))->value('serviceCategoryId');
                $providerList = Db::name('member')
                    ->join('provider','member.memberId=provider.providerId')
                    ->where(array('areaId'=>$diId,'memberType'=>1,'providerType'=>0))->column('memberId');
                $lvShiList = array();
                foreach($providerList  as $k=>$v){
                    //查询包含法律服务的服务商
                    $provider_group =  Db::name('provider_group')
                        ->where(array('providerId'=>$v))
                        ->column('provideGroupId');
                    if(in_array($lvShiId,$provider_group)){
                        array_push($lvShiList,$v);
                    }
                }
                if(count($lvShiList) ==1 ){
                    $industryExperience =  Db::name('provider')->where(array('providerId'=>$lvShiList['0']))->value('industryExperience');
                    $headImgFileId   = DB::name('member')->where('memberId',$lvShiList['0'])->value('headImgFileId');
                    $realName = DB::name('provider_data')->where('providerId',$lvShiList['0'])->value('realName');
                    $detailImgFile = filepath($headImgFileId);
                    $detailImgType =  '律师';
                    $data['detailImgMember']['industryExperience'] = $industryExperience;
                    $data['detailImgMember']['memberId'] = $lvShiList['0'];
                    $data['detailImgMember']['realName'] = $realName;
                }else{
                    //查找服务的图标
                    $detailImgFile = Db::name('service')
                        ->join('files','service.iconFileId=files.filesId')
                        ->where('serviceId',$serviceId)
                        ->value('detailImgFile');
                    $detailImgType='产品';
                    $data['detailImgMember'] ='';
                }
                $data['ImgFile']  = $detailImgFile;
                $data['detailImgType'] = $detailImgType;
            }
        }
        return $data;
    }
}